<?php
class MIS_Controller extends CI_Controller 
{
	public $loadViewData = array('viewCSS' => array(), 'viewJS' => array(), 'viewTITLE' => '');
	public $is_loggedin = false;
	public $is_admin = false;
	
    function __construct()
    {
        parent::__construct();
		
		// Extra Config Settings
		//$this->config->load('site_settings', FALSE, TRUE);
		//$this->config->set_item('site_name', 'TheRuGuru');
		$res = $this->db->get_where('site_settings', array('necessity' => 'common'));
		if($res->num_rows() > 0)
		{
			foreach($res->result() as $info) $this->config->set_item($info->item_name, $info->item_value);
			$res->free_result();
		}
		
		// Logged in settings
		$this->loadViewData['is_loggedin'] = $this->is_loggedin = ($this->session->userdata('id_user') ? TRUE : FALSE);
		$this->loadViewData['is_admin'] = $this->is_admin = ($this->session->userdata('is_admin') ? TRUE : FALSE);
		
		// View related common config
		$this->loadViewData['inner_header_inc'] = "includes/inner_header_inc";
		$this->loadViewData['banner_inc'] = "includes/banner_inc";
		$this->loadViewData['search_inc'] = "includes/inner-search-form";
		$this->loadViewData['viewFile'] = "blank";
		$this->loadViewData['upper_footer_inc'] = "includes/upper_footer_inc";
		$this->loadViewData['footer_inc'] = "includes/footer_inc";
		
		//	Settings of default title
		$this->loadViewData['viewTITLE'] = $this->config->item('site_name');
		$this->addCss("css/global.css");
    }
	
	private function updateLastVisited()
	{
		if($this->is_loggedin && !$this->is_admin) $this->m_user->updateLastVisited();
	}
	
	public function sendHTMLMail($from, $to, $subject, $mesg)
	{
		$this->load->library('email');
		$this->email->clear();
		$this->config->load('site_config', FALSE, TRUE);
		$this->email->initialize($this->config->item('email'));

		$this->email->to($to);
		$this->email->from($from);
		$this->email->subject($subject);
		$this->email->message($mesg);
		try
		{
			$this->email->send();
		}
		catch(Exception $ex){}
	}
	
	function redirectAfterLogin($uri = FALSE, $default = 'welcome')
	{
		if($uri !== FALSE) $this->session->set_userdata('redirect_after_login', $uri);
		else redirect($this->session->userdata('redirect_after_login')?$this->session->userdata('redirect_after_login'):$default);
	}
	
	public function getRandomNumber($length = 32)
	{
		$str = $strL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
		$strlen = strlen($str);
		while($length > strlen($strL)) $strL .= $str;
		$str = str_shuffle($strL);
		return substr($str, 0, $length);
	}
	
	public function addJs($file)
	{
		$this->loadViewData['viewJS'][] = $file;
	}
	
	public function addCss($file)
	{
		$this->loadViewData['viewCSS'][] = $file;
	}
	
	public function addTitle($str, $onlyThis = false, $right = true)
	{
		if($onlyThis === FALSE) $this->loadViewData['viewTITLE'] = $str;
		else
		{
			if($right === FALSE) $this->loadViewData['viewTITLE'] = sprintf("%s -- %s", $str, $this->loadViewData['viewTITLE']);
			else $this->loadViewData['viewTITLE'] = sprintf("%s -- %s", $this->loadViewData['viewTITLE'], $str);
		}
	}
	
	public function loadRecaptcha()
	{
		$config = array(
			'public'						=> $this->config->item('recaptcha_public_key'),
			'private'						=> $this->config->item('recaptcha_private_key'),
			'RECAPTCHA_API_SERVER' 			=> 'http://www.google.com/recaptcha/api',
			'RECAPTCHA_API_SECURE_SERVER'	=> 'https://www.google.com/recaptcha/api',
			'RECAPTCHA_VERIFY_SERVER' 		=> 'www.google.com',
			'RECAPTCHA_SIGNUP_URL' 			=> 'https://www.google.com/recaptcha/admin/create',
			'theme' 						=> 'red'
		);
		
		$this->config->set_item('recaptcha', $config);
		$this->load->library('form_validation');
		$this->load->library('recaptcha');
		$this->lang->load('recaptcha');
		$this->form_validation->set_rules('recaptcha_response_field', 'Recaptcha', 'callback_check_captcha');
	}
	
	function check_captcha($val)
	{
		if ($this->recaptcha->check_answer($this->input->ip_address(),$this->input->post('recaptcha_challenge_field'),$val)) return TRUE;
		else 
		{
			$this->form_validation->set_message('check_captcha',$this->lang->line('recaptcha_incorrect_response'));
			return FALSE;
		}
	}
	
	function get_config($item_name = false, $necessity = false)
	{
		$data = FALSE;
		if($item_name) $this->db->where('item_name', $item_name);
		if($necessity !== false) $this->db->where('necessity', $necessity);
		$res = $this->db->get('site_settings');
		$count = $res->num_rows();
		if($count > 0) 
		{
			if($count == 1)
			{
				$info = $res->row();
				$data = $info->item_value;
			}
			else
			{
				$data = array();
				foreach($res->result() as $info) $data[$info->item_name] = $info->item_value;
			}
		}
		return $data;
	}
}
